#include "BlockQueue.hpp"


void *consumer(void *arg){ 
    BlockQueue<int> *bq = (BlockQueue<int> *)arg;
    while(true){
        // 消费
        int data = bq->pop();
        cout << "消费：" << data << endl;
        sleep(1);  // 生产者消费者的sleep可以控制 速度
    }
}

void *producer(void *arg){
    BlockQueue<int> *bq = (BlockQueue<int> *)arg;
    int data = 1;
    while(true){
        // 生产
        bq->push(data);
        cout << "生产：" << data << endl;
        data++;
        sleep(1);
    }
}

int main()
{
    BlockQueue<int> *bq = new BlockQueue<int>(5);   // 中间缓冲区
    pthread_t c[3], p[5];  // 俩线程

    for ( int i = 0; i < 5; i++){
        pthread_create(&p[i], NULL, producer, bq);
    }
    for ( int i = 0; i < 3; i++){
        pthread_create(&c[i], NULL, consumer, bq);  // 消费
    }



    for (int  i = 0; i < 5; i++){
         pthread_join(p[i], NULL);
    }
    for (int  i = 0; i < 3; i++){
         pthread_join(c[i], NULL);
    }

    return 0;
}